#include <stdio.h>

int main(){
    int N;
    scanf("%d", &N);
    
    for (int i = 0; i < N; i++){
        int day, hh, mm;
        scanf("%d %d:%d", &day, &hh, &mm);
        
        if(day == 0){
            printf("%d %02d:%02d\n", day, hh, mm);
            continue;
        }
        
        int a = (day - 1) * 1440 + (hh * 60 + mm);
        int b = a / 2;
        
        int c = b / 1440 + 1;  
        int d = b % 1440;
        int e = d / 60;
        int g = d % 60;
        
        printf("%d %02d:%02d\n", c, e, g);
    }
    
    return 0;
}
